﻿using System;
using DataLayer.Types;

namespace ViewModel
{
    public class SavingsPlanCalculator
    {
        public static decimal CalculateAllotmentAmount(DateTime startDate, DateTime endDate, 
            decimal targetAmount, decimal initialAmount, FrequencyItem frequency)
        {
            if (frequency == null) return 0;

            decimal amountRequired = targetAmount - initialAmount;

            TimeSpan ts = endDate - startDate;

            decimal sub = amountRequired / (decimal)ts.TotalDays;

            return sub * frequency.ValueInDays;
        }

        public static decimal CalculateBalance(DateTime startDate, DateTime endDate, DateTime currentDate,
            decimal targetAmount, decimal initialAmount, decimal retrievedAmount)
        {
            decimal amountRequired = targetAmount - initialAmount;

            TimeSpan ts = endDate - startDate;

            decimal sub = amountRequired / (decimal)ts.TotalDays;

            DateTime ceiling = currentDate >= endDate ? endDate : currentDate;

            TimeSpan ts2 = ceiling - startDate;

            return (sub * (decimal)ts2.TotalDays) - retrievedAmount;
        }
    }
}
